//
//  HomeViewController.swift
//  Weibo10
//
//  Created by Leon on 15/10/14.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import SVProgressHUD

/// 原创微博 CellID
let StatusNormalCellReuseID = "StatusNormalCellReuseID"
/// 转发微博 CellID
let StatusRetweetedCellReuseID = "StatusRetweetedCellReuseID"


class HomeViewController: VisitorViewController {
    
    
    private var listViewModel = StatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        if !UserAccountViewModel.shareAccount.userLogon {
            visitorView?.setupInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        loadData()
        prepareTableView()
    }
    /// 准备表格
    private func prepareTableView() {
        // 注册 cell
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: StatusRetweetedCellReuseID)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusNormalCellReuseID)

        // 自动调整行高
        tableView.estimatedRowHeight = 300
        
        // 隐藏分割线
        tableView.separatorStyle = .None
        
        // 刷新控制器
        refreshControl = StatusRefreshControl()
        
        // 添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)

        // 设置 footerView
        tableView.tableFooterView = pullUpdateView
    }
    /// 加载数据
    @objc private func loadData() {
        
        // 开始刷新控件
        refreshControl?.beginRefreshing()
        
        listViewModel.loadStatus(isPulled: pullUpdateView.isAnimating() ) { (isSuccessed) -> () in
            
            // 关闭刷新控件
            self.refreshControl?.endRefreshing()
            // 关闭上拉刷新
            self.pullUpdateView.stopAnimating()
            
            if !isSuccessed {
                SVProgressHUD.showInfoWithStatus("你的网络挂了")
                return
            }
            
            // 刷新数据
            self.tableView.reloadData()
        }
    }
   
    // MARK: - 上拉刷新
    private var pullUpdateView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.color = UIColor.lightGrayColor()
        
        return indicator
        
    }()
}

// MARK: - 数据源方法
extension HomeViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        // 1 取出模型
        let vm = listViewModel.statusList[indexPath.row]
        
        // 2 获取相关的Cell
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellID, forIndexPath: indexPath) as! StatusCell
        
        
        // 判断是否下拉刷新
        if indexPath.row == listViewModel.statusList.count - 1 && !pullUpdateView.isAnimating(){
            pullUpdateView.startAnimating()
            
            loadData()
            
            print("上拉刷新数据")
        }
        
        cell.viewModel = vm
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return listViewModel.statusList[indexPath.row].rowHeight
    }
}














